// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/*
 * 主要控制类 
 * 山东11选5投注界面 
 */
cc.Class({
    extends: cc.Component,

    properties: {
        labPlaying:cc.Label, //当前玩法名
        ndBetBg: cc.Sprite, 
        spBetBg:[cc.SpriteFrame], //投注背景
        pvBet:cc.Node,  //pageview
        edMutipleNum: cc.Label,  //投注金额
        curMoney:0,
        curBetNum:0,  
        pageArr: [cc.Node],  //投注面板
        tgSelect: cc.Toggle,  //机选,清空
        numberKeysPre: cc.Prefab,  //键盘界面预制

        _ruleName: [],
        _page: null,
        _pageManage: null,
        _lotteryCode:0, //彩种id
        _pageIndex:0,
        _keysOpen: false  

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            //('bet115 TOUCH_START-' + this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
            //cc.log('bet115 MOUSE_DOWN-' + this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            //cc.log('bet115 TOUCH_MOVE-'+ this.node.name);  
        }.bind(this), this);
        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            //cc.log('bet115 TOUCH_END-'+ this.node.name);  
        }.bind(this), this);  
    },

    start () {
        this._page = this.pvBet.getComponent("PageViewExt");  
        this._ruleName =['任选','直选','组选','乐选'];
        this.labPlaying.string =this._ruleName[0];  

        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            var contentRect = this.ndBetBg.node.getBoundingBoxToWorld();
            var touchLocation = event.getLocation();
            contentRect.x = contentRect.x -20;
            contentRect.width = 1080;
            if(cc.rectContainsPoint(contentRect, touchLocation) == false){//关闭投注界面
                this.onClose();
            }
        }, this);
    },

    init: function(lotteryCode){
        this._lotteryCode = lotteryCode;    
        
    },

    pageViewClick:function(event,customEventData){
        this._pageIndex = event.node.getComponent("PageViewExt").getCurrentPageIndex();
        this.labPlaying.string =this._ruleName[this._pageIndex];  
        if(this._page.getCurrentPageIndex() ==0){
            this.ndBetBg.spriteFrame = this.spBetBg[0];   //背景
        }else{
            this.ndBetBg.spriteFrame = this.spBetBg[1]; 
        }
        return;
    },

    //右
    onRightPage:function(event,customEventData){
        this._pageIndex = this._page.getCurrentPageIndex()+1 == this._page.getPages().length ? 0 : this._page.getCurrentPageIndex()+1;
        this._page.scrollToPage(this._pageIndex);
    },

    //左
    onLeftPage:function(event,customEventData){
        this._pageIndex = this._page.getCurrentPageIndex()-1 <0 ? this._page.getPages().length-1 : this._page.getCurrentPageIndex()-1;
        this._page.scrollToPage(this._pageIndex);
    },

    //设置倍数
    setMutipleAmount:function(mutiple){
        this.edMutipleNum.string = mutiple;
       
    },

    //获取当前倍数
    getMutipleAmount:function(){
        var amount = parseInt(this.edMutipleNum.string);
        if(isNaN(amount)){ 
            return 1;
        }else{
            return amount;
        }   
    },

    //设置金额
    setMoney:function(mon){
        this.curMoney = mon;
    },

    //得到金额
    getMoney:function(){
        return this.curMoney;
    },

    //设置注数
    setBetNum:function(num){
        this.curBetNum = num;
    },

    //得到注数
    getBetNum:function(){
        return this.curBetNum;
    },

    onTextBegan: function(){
        this.emptyNode =this.node.getChildByName('Node');
        if(!this.emptyNode.getChildByName('public_numberKeys_pop')){  //当键盘存在，就不必创建
            this.KeysPre =cc.instantiate(this.numberKeysPre);
            this.emptyNode.addChild(this.KeysPre);
            var Callback = function(data){
            this.onEditBoxMutipleChanged(data);
            }.bind(this);
            this.KeysPre.getComponent(this.KeysPre.name).init(Callback);
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面  
        }else{
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面      
        }
        this._keysOpen =true;  //键盘开启
    },

    //手动倍数
    onEditBoxMutipleChanged:function(text) {
        var that =this.pageArr[this._page.getCurrentPageIndex()].getComponent(this.pageArr[this._page.getCurrentPageIndex()].name);
        var amount =text;
        if(amount =='ok'){
            // this.emptyNode.getComponent(cc.Layout).enabled = false; 
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面  
            this._keysOpen =false;
            if(this.edMutipleNum.string == null || this.edMutipleNum.string == "")
            {
                this.edMutipleNum.string = "1";
            }
        }else if(amount =='-'){
            if(this.edMutipleNum.string.length > 0){
                this.edMutipleNum.string =this.edMutipleNum.string.slice(0,-1);  
            }        
        }else{
            this.edMutipleNum.string += amount;  
            if(this.edMutipleNum.string.length>5){   //只能输入5位
                this.edMutipleNum.string =this.edMutipleNum.string.slice(0,5);  
            }
        }
        
        that.checkBet();   
    },

    //遗漏
    onMiss:function(toggle){
        var that =this.pageArr[this._page.getCurrentPageIndex()].getComponent(this.pageArr[this._page.getCurrentPageIndex()].name);
        if(that.isMiss && toggle.getComponent(cc.Toggle).isChecked)
            return;
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            that.isMiss = true;
            switch(that.name){
                case 'bet_115_anySelect<bet_115_anySelect>':
                    that.showMiss(that.betToggleList,this._lotteryCode,this._lotteryCode+that.rules[that.curPage-2].toString());
                break;
                case 'bet_115_directSelect<bet_115_directSelect>':
                    that.showMissArry(that.betToggleList,this._lotteryCode,this._lotteryCode+that.rules[that.curPage-1].toString());
                break;
                case 'bet_115_groupSelect<bet_115_groupSelect>':
                    that.showMiss(that.betToggleList,this._lotteryCode,this._lotteryCode + that.rules[that.curPage-2].toString());
                break;
                case 'bet_115_leSelect<bet_115_leSelect>':
                    if(that.curPage-3>0)
                    {
                        that.showMiss(that.betToggleList[0],this._lotteryCode,this._lotteryCode+that.rules[that.curPage-3].toString());
                    }
                    else
                    {
                        that.showMissArry(that.betToggleList,this._lotteryCode,this._lotteryCode+that.rules[that.curPage-3].toString());
                    }  
                break;
                default:
                    return;
                break;
            }
        }    
        else
        {
            that.isMiss = false;
            switch(that.name){
                case 'bet_115_anySelect<bet_115_anySelect>':
                    that.setMiss(false,that.betToggleList,"");
                break;
                case 'bet_115_directSelect<bet_115_directSelect>':
                    that.setMissArry(false,that.betToggleList,"");
                break;
                case 'bet_115_groupSelect<bet_115_groupSelect>':
                    that.setMiss(false,that.betToggleList,"");
                break;
                case 'bet_115_leSelect<bet_115_leSelect>':
                    that.setMissArry(false,that.betToggleList,"");   
                break;
                default:
                    return;
                break;
            }
        }
    },

    //选择清空切换
    onSelectCallBack:function(toggle){
        var that =this.pageArr[this._page.getCurrentPageIndex()].getComponent(this.pageArr[this._page.getCurrentPageIndex()].name);
        if(that.isNorm == 0)
        {
            toggle.getComponent(cc.Toggle).isChecked = true;
            that.clearCurSel();
        }
        else
        {
            if(toggle.getComponent(cc.Toggle).isChecked)
            {
                that.randomSelectCallBack();
            }
            else
            {
                that.clearCurSel();
            }
        }
    },

    //付款
    onPayBtn:function(){
        if(this.edMutipleNum.string == null || this.edMutipleNum.string == "")
        {
            this.edMutipleNum.string = "1";
        }
        if(this.getMoney() <= 0)
            return; 
      
        var that =this.pageArr[this._page.getCurrentPageIndex()].getComponent(this.pageArr[this._page.getCurrentPageIndex()].name)
        var data = {
            lotteryId:this._lotteryCode,//彩种id
            dataBase: that.dataToJson(),//投注信息
            otherBase:"",//追加
            money:this.getMoney(),
            buyType: 0,//追号
            betType: DEFINE.BET_PAY_TYPE.NORMAL//投注类型
        }
        window.Notification.emit("BET_ONPAY",data);

        //关闭键盘界面
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){  
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
            this._keysOpen =false;
        }
    },

    onClose:function(){
        window.Notification.emit("BET_ONCLOSE","");
        //关闭键盘界面
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){  
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
            this._keysOpen =false;
        } 
    },

    // update (dt) {},
});
